/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.hboxs.asl.Setting;
import com.hboxs.asl.util.SettingUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Map;

/**
 * Entity - 购物车项
 */
@Entity
@Table(name = "xx_cart_item")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_cart_item_sequence")
public class CartItem extends BaseEntity {

    private static final long serialVersionUID = 2979296789363163144L;

    /**
     * 最大数量
     */
    public static final Integer MAX_QUANTITY = 10000;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 商品
     */
    private Product product;

    /**
     * 购物车
     */
    private Cart cart;

    /**
     * 临时商品价格
     */
    private BigDecimal tempPrice;

    /**
     * 临时赠送积分
     */
    private BigDecimal tempPoint;

    /**
     * 选中标记创建购物车
     */
    private boolean isSelected;

    /**
     * 历史购买价
     */
    private BigDecimal dealPrice;

    /**
     * 历史商家赠送积分
     */
    private BigDecimal dealPoint;

    /**
     * 历史商家赠送会员积分
     */
    private BigDecimal dealmPoint;

    /**
     * 主播
     */
    private Member anchor;

    /**
     * 获取数量
     *
     * @return 数量
     */
    @Column(nullable = false)
    public Integer getQuantity() {
        return quantity;
    }

    /**
     * 设置数量
     *
     * @param quantity 数量
     */
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Member getAnchor() {
        return anchor;
    }

    public void setAnchor(Member anchor) {
        this.anchor = anchor;
    }

    /**
     * 获取商品
     *
     * @return 商品
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false, updatable = false)
    public Product getProduct() {
        return product;
    }

    /**
     * 设置商品
     *
     * @param product 商品
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * 获取购物车
     *
     * @return 购物车
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false)
    public Cart getCart() {
        return cart;
    }

    /**
     * 设置购物车
     *
     * @param cart 购物车
     */
    public void setCart(Cart cart) {
        this.cart = cart;
    }

    /**
     * 获取选择标记
     *
     * @return 选择标记
     */
    @NotNull
    @Column(nullable = false)
    public boolean getIsSelected() {
        return isSelected;
    }

    /**
     * 设置选择标记
     *
     * @param selected 选择标记
     */
    public void setIsSelected(boolean selected) {
        isSelected = selected;
    }

    /**
     * 获取购买价
     *
     * @return 购买价
     */
    public BigDecimal getDealPrice() {
        return dealPrice;
    }

    /**
     * 设置购买价
     *
     * @param dealPrice 购买价
     */
    public void setDealPrice(BigDecimal dealPrice) {
        this.dealPrice = dealPrice;
    }

    /**
     * 获取历史商家赠送积分
     *
     * @return 历史商家赠送积分
     */
    public BigDecimal getDealPoint() {
        return dealPoint;
    }

    /**
     * 设置历史商家赠送积分
     *
     * @param dealPoint 历史商家赠送积分
     */
    public void setDealPoint(BigDecimal dealPoint) {
        this.dealPoint = dealPoint;
    }

    /**
     * 获取历史商家赠送会员积分
     *
     * @return 历史商家赠送会员积分
     */
    public BigDecimal getDealmPoint() {
        return dealmPoint;
    }

    /**
     * 设置历史商家赠送会员积分
     *
     * @param dealmPoint 历史商家赠送会员积分
     */
    public void setDealmPoint(BigDecimal dealmPoint) {
        this.dealmPoint = dealmPoint;
    }

    /**
     * 获取临时商品价格
     *
     * @return 临时商品价格
     */
    @Transient
    public BigDecimal getTempPrice() {
        if (tempPrice == null) {
            return getSubtotal();
        }
        return tempPrice;
    }

    /**
     * 设置临时商品价格
     *
     * @param tempPrice 临时商品价格
     */
    @Transient
    public void setTempPrice(BigDecimal tempPrice) {
        this.tempPrice = tempPrice;
    }

    /**
     * 获取临时赠送积分
     *
     * @return 临时赠送积分
     */
    @Transient
    public BigDecimal getTempPoint() {
        if (tempPoint == null) {
            return getPoint();
        }
        return tempPoint;
    }

    /**
     * 设置临时赠送积分
     *
     * @param tempPoint 临时赠送积分
     */
    @Transient
    public void setTempPoint(BigDecimal tempPoint) {
        this.tempPoint = tempPoint;
    }

    /**
     * 获取赠送积分
     *
     * @return 赠送积分
     */
    @Transient
    public BigDecimal getPoint() {
        if (getDealPoint() != null) {
            return getDealPoint().multiply(new BigDecimal(getQuantity()));
        } else if (getProduct() != null && getProduct().getPoint() != null && getQuantity() != null) {
            return getProduct().getPoint().multiply(new BigDecimal(getQuantity()));
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 获取赠送给会员的积分
     *
     * @return 赠送积分
     */
    @Transient
    public BigDecimal getmPoint() {
        if (getDealmPoint() != null) {
            return getDealmPoint().multiply(new BigDecimal(getQuantity()));
        } else if (getProduct() != null && getProduct().getmPoint() != null && getQuantity() != null) {
            return getProduct().getmPoint().multiply(new BigDecimal(getQuantity()));
        } else {
            return new BigDecimal(0);
        }
    }


    /**
     * 获取商品重量
     *
     * @return 商品重量
     */
    @Transient
    public int getWeight() {
        if (getProduct() != null && getProduct().getWeight() != null && getQuantity() != null) {
            return getProduct().getWeight() * getQuantity();
        } else {
            return 0;
        }
    }

    /**
     * 获取价格
     *
     * @return 价格
     */
    @Transient
    public BigDecimal getPrice() {
        Setting setting = SettingUtils.get();
        if (getDealPrice() != null) {
            return setting.setScale(getDealPrice());
        } else if (getProduct() != null && getProduct().getPrice() != null) {
            if (getCart() != null && getCart().getMember() != null && getCart().getMember().getMemberRank() != null) {
                MemberRank memberRank = getCart().getMember().getMemberRank();
                Map<MemberRank, BigDecimal> memberPrice = getProduct().getMemberPrice();
                if (memberPrice != null && !memberPrice.isEmpty()) {
                    if (memberPrice.containsKey(memberRank)) {
                        return setting.setScale(memberPrice.get(memberRank));
                    }
                }
                if (memberRank.getScale() != null) {
                    setting.setPriceScale(new Integer(memberRank.getScale().intValue()));
                    setting.setPriceScale(2);
                    return setting.setScale(getProduct().getPrice());
                }
            }
            return setting.setScale(getProduct().getPrice());
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 获取小计
     *
     * @return 小计
     */
    @Transient
    public BigDecimal getSubtotal() {
        if (getQuantity() != null) {
            return getPrice().multiply(new BigDecimal(getQuantity()));
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 获取是否库存不足
     *
     * @return 是否库存不足
     */
    @Transient
    public boolean getIsLowStock() {
        return getQuantity() != null && getProduct() != null && getProduct().getStock() != null && getQuantity() >= getProduct().getAvailableStock();
    }

    /**
     * 增加商品数量
     *
     * @param quantity 数量
     */
    @Transient
    public void add(int quantity) {
        if (quantity > 0) {
            if (getQuantity() != null) {
                setQuantity(getQuantity() + quantity);
            } else {
                setQuantity(quantity);
            }
        }
    }

    /**
     * 价格对比
     *
     * @return
     */
    @Transient
    public String getComparablePrice() {
        Setting setting = SettingUtils.get();
        String compare = "";
        BigDecimal currentPrice = this.product.getPrice();
        BigDecimal originalPrice = this.getDealPrice();
        if (currentPrice.compareTo(originalPrice) > 0) {
            compare = "Current price $" + setting.setScale(currentPrice) + " more than $" + setting.setScale(currentPrice.subtract(originalPrice));
        } else if (currentPrice.compareTo(originalPrice) < 0) {
            compare = "Current price $" + setting.setScale(currentPrice) + " less than $" + setting.setScale(originalPrice.subtract(currentPrice));
        } else {
            compare = "";
        }
        return compare;
    }


}